package handler

import (
	"github.com/gin-gonic/gin"
	"prod-manage/models"
	"strconv"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if title == "" || content == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	product := models.Product{
		Title:   title,
		Content: content,
		Cid:     cid,
	}
	err := models.DB.Create(&product).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
	return
}
func List(c *gin.Context) {
	var cate []models.Cate
	models.DB.Find(&cate)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": cate,
	})
	return
}
func ProductList(c *gin.Context) {
	var product []models.Product
	models.DB.Find(&product)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": product,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var product models.Product
	models.DB.Where("id = ?", id).First(&product)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": product,
	})
	return
}
